package guava.study.lyh.other;

import com.google.common.base.*;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;
import com.sun.istack.internal.Nullable;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 作者 yaohua.liu
 * 时间 2014-07-08 14:07 test="   ";
 * Assert.assertTrue(isNullOrEmpty(test));
 * 包名 com.qunar.fresh.lyh
 * 说明 1. 基本工具 [Basic utilities]
 * 让使用Java语言变得更舒适
 * <p/>
 * 1.1 使用和避免null：null是模棱两可的，会引起令人困惑的错误，有些时候它让人很不舒服。很多Guava工具类用快速失败拒绝null值，而不是盲目地接受
 * <p/>
 * 1.2 前置条件: 让方法中的条件检查更简单
 * <p/>
 * 1.3 常见Object方法: 简化Object方法实现，如hashCode()和toString()
 * <p/>
 * 1.4 排序: Guava强大的”流畅风格比较器”
 * <p/>
 * 1.5 Throwables：简化了异常和错误的传播与检查
 */

public class Guava_1 {

    @Test
    public void testNULL(){
        String test=null;
        Assert.assertTrue(Strings.isNullOrEmpty(test));
        test="";
        Assert.assertTrue(Strings.isNullOrEmpty(test));
    }

    @Test
    public void testPreconditions(){
        int i=11;
        int j=10;
        Preconditions.checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);
        Preconditions.checkArgument(i < j, "Expected i < j, but %s >= %s", i, j);
    }

    @Test
    public void testObjects(){
        Assert.assertTrue(Objects.equal(null, null)); // returns true
        Assert.assertTrue(Objects.equal("a", "a")); // returns true
        Assert.assertTrue(Objects.equal("a", "b")); // returns true
        Assert.assertTrue(Objects.equal(null, "a")); // returns false
        Assert.assertTrue(Objects.equal("a", null)); // returns false
    }

    @Test
    public void testHashCode(){
        System.out.println(Objects.hashCode("a"));
        System.out.println(Objects.hashCode("b"));
        System.out.println(Objects.hashCode("a", "b"));
    }

    @Test
    public void testToString(){
        // Returns "ClassName{x=1}"
        System.out.println(Objects.toStringHelper(this).add("x", 1).toString());
        System.out.println(Objects.toStringHelper(Guava_1.class).add("x", 1).toString());
        // Returns "MyObject{x=1}"
        System.out.println(Objects.toStringHelper("MyObject").add("x", 1).toString());
    }

    @Test
    public void testComparable(){
        Foo person=new Foo();
        List<Foo> persons=new ArrayList<Foo>();
        person.sortedBy="aa";
        person.notSortedBy=1;
        persons.add(person);
        person.sortedBy="bb";
        persons.add(person);
        Collections.sort(persons);
    }

    class Foo implements Comparable<Foo>{
        @Nullable String sortedBy;
        int notSortedBy;
        public int compareTo(Foo that) {
            return ComparisonChain.start()
                    .compare(this.sortedBy, that.sortedBy)
                    .compare(this.notSortedBy, that.notSortedBy)
                            //.compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
                    .result();
        }
    }

    @Test
    public void testOrdering(){
        Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() {
            public String apply(Foo foo) {
                return foo.sortedBy;
            }
        });
    }

    @Test
    public void testThrowables(){
        Throwables.propagate(new IOException("这里是测试异常。。。"));
    }

}
